package com.xmall.service.impl;

import com.github.pagehelper.PageInfo;
import com.xmall.bean.table.*;
import com.xmall.bean.vo.DashboardVo;
import com.xmall.bean.vo.IssueList;
import com.xmall.bean.vo.AdminInfoVo;
import com.xmall.mapper.*;
import com.xmall.service.AdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
@Service
public class AdminServiceImpl implements AdminService {

    @Autowired
    AdminMapper adminMapper;

    @Autowired
    RoleMapper roleMapper;

    @Autowired
    PermissionMapper permissionMapper;

    @Autowired
    GoodsMapper goodsMapper;

    @Autowired
    UserMapper userMapper;

    @Autowired
    GoodsProductMapper goodsProductMapper;

    @Autowired
    OrderMapper orderMapper;

    @Override
    public IssueList<Admin> selectAdmin(Integer page, Integer limit, String username, String sort, String order) {
        AdminExample adminExample = new AdminExample();


        IssueList<Admin> adminIssueList = new IssueList<>();
        AdminExample.Criteria criteria = adminExample.createCriteria();
        if(username != null){
            criteria.andUsernameLike("%" + username + "%");
        }
        criteria.andDeletedEqualTo(false);

        PageInfo<Admin> adminPageInfo = new PageInfo<>();
        List<Admin> admins = adminMapper.selectByExample(adminExample);


        long total = adminPageInfo.getTotal();
        adminExample.setOrderByClause(sort + " " + order);

        adminIssueList.setItems(admins);
        adminIssueList.setTotal(total);
        return adminIssueList;
    }

    @Override
    public Admin insertAdmin(Admin admin) {
        AdminExample adminExample = new AdminExample();
        AdminExample.Criteria criteria = adminExample.createCriteria();
        criteria.andDeletedEqualTo(false);
        admin.setAddTime(new Date());
        admin.setUpdateTime(new Date());
        adminMapper.insertSelective(admin);
        return admin;
    }

    @Override
    public Admin updateAdmin(Admin admin) {
        admin.setUpdateTime(new Date());
        return admin;
    }

    @Override
    public Integer deleteAdmin(Admin admin) {
        admin.setDeleted(true);
        int update = adminMapper.updateByPrimaryKey(admin);
        return update;
    }

    @Override
    public AdminInfoVo queryAdminInfoByUsername(String username) {
        AdminExample adminExample = new AdminExample();
        AdminExample.Criteria criteria = adminExample.createCriteria();
        criteria.andUsernameEqualTo(username);
        List<Admin> admins = adminMapper.selectByExample(adminExample);
        Integer[] roleIds = admins.get(0).getRoleIds();
        String avatar = admins.get(0).getAvatar();
        List<String> roleNames = roleMapper.queryRoleNameByRoleIds(roleIds);
        List<String> permissions = permissionMapper.queryPermissionsByRoleIds(roleIds);
        List<String> apiList = new ArrayList<>();
        if ("*".equals(permissions.get(0))) {
            apiList.add("*");
        } else {
            apiList = permissionMapper.queryApiByPermission(permissions);
        }
        return new AdminInfoVo(username, avatar, roleNames, apiList);
    }

    @Override
    public DashboardVo queryDashBoardTotal() {
        GoodsExample goodsExample = new GoodsExample();
        int goodsTotal = (int) goodsMapper.countByExample(goodsExample);
        UserExample userExample = new UserExample();
        int userTotal = (int) userMapper.countByExample(userExample);
        GoodsProductExample goodsProductExample = new GoodsProductExample();
        int productTotal = (int) goodsProductMapper.countByExample(goodsProductExample);
        OrderExample orderExample = new OrderExample();
        int orderTotal = (int) orderMapper.countByExample(orderExample);
        return new DashboardVo(goodsTotal, userTotal, productTotal, orderTotal);
    }


}
